attributes <- results$attributes

attributes$Economy[1] <- "Eliminate the deficit [Cons, Lab, Libdem, UKIP]"
attributes$Economy[2] <- "End austerity  [Greens, (SNP)]"
attributes$Jobs[1] <- "Extend apprenticeships and vocational training [Cons, Libdem]"
attributes$Jobs[2] <- "Prioritise British citizens for jobs [UKIP]"
attributes$Jobs[3] <- "Increase minimum wage [Labs, Greens, (SNP)]"
attributes$Immigration[1] <- "Four-year wait before EU migrants can claim benefits [Cons]"
attributes$Immigration[2] <- "Introduce point system to select skilled migrants. Five-year wait... [UKIP]"
attributes$Immigration[3] <- "Progressively remove migration controls [Greens]"
attributes$Immigration[4] <- "Restore full entry and exit border checks [Libdem]"
attributes$Immigration[5] <- "Two-year wait before EU migrants can claim benefits [Lab]"
attributes$EU[1] <- "Hold a referendum on Britain's EU membership [Cons, Greens]"
attributes$EU[2] <- "Leave the EU [UKIP]"
attributes$EU[3] <- "Reform the EU and keep Britain in it [Lab, Libdem, (SNP)]"
attributes$Education[1] <- "Create more free schools [Libdem, Cons]"
attributes$Education[2] <- "Cut university tuition fees by a third [Lab]"
attributes$Education[3] <- "No university tuition fees for all students [Greens, (SNP)]"
attributes$Education[4] <- "No university tuition fees for natural science students [UKIP]"

att.tab <- NULL
for(i in 1:length(attributes)){
  category <- sort(names(attributes))[i]
  for(j in 1:length(attributes[[category]])){
    temp <- ifelse(j == 1, category, "")
    str <- attributes[[category]][j]
    lev <- sub("(.+)\\s+\\[.+", "\\1", str)
    pty <- sub(".+\\s+\\[(.+)\\]", "\\1", str)
    temp <- c(temp, lev, pty)
    att.tab <- rbind(att.tab, temp)
  }
}
row.names(att.tab) <- NULL

att.tab2 <- NULL
for (i in 1:length(attributes)) {
  category <- sort(names(attributes))[i]
  for (j in 1:length(attributes[[category]])) {
    temp <- sprintf("%s%s",category, LETTERS[j])
    
    str <- attributes[[category]][j]
    lev <- str #sub("(.+)\\s+\\[.+", "\\1", str)
    if (j == 1) {
      lev <- sprintf("%s: (base = %s)", category, lev)
    } else {
      lev <- sprintf("    %s", lev)
    }
    temp <- c(temp, lev)
    att.tab2 <- rbind(att.tab2, temp)
  }
}
row.names(att.tab2) <- NULL
att.data2 <- data.frame(att.tab2)
names(att.data2) <- c("var2", "att_content")

process_eachitem <- function(x){
  estimates <- data.frame(rbind(rep(NA, 2), c(0, 0), t(x)))
}
temp <- lapply(results$estimates, process_eachitem)
amce_plot_data <- rbindlist(temp)
names(amce_plot_data)[2] <- "se"
amce_plot_data$y <- nrow(amce_plot_data):1

texts <- cbind(paste0(att.tab[, 1], ":"), paste("    ", att.tab[, 2], sprintf("[%s]  ", att.tab[, 3]))) %>%
  t %>% as.vector
texts <- texts[grep("^:", texts, invert = TRUE)]
amce_plot_data$texts <- factor(nrow(amce_plot_data):1, labels = rev(texts))

g <- ggplot(amce_plot_data, aes(x = AMCE, y = texts)) + 
  geom_vline(xintercept = 0, size = 1, color = "grey90") + 
  geom_point() + 
  geom_errorbarh(aes(xmin = AMCE - 1.96 * se,
                     xmax = AMCE + 1.96 * se, height = 0)) + 
  ylab("") + xlab("AMCE") + 
  theme(axis.text.y = element_text(hjust = 0))
